﻿var awf = function() {
    
    function r(o, l) { $.each(o.data.vals, function(i, k) { $('#' + k).bind('change awfready', l); }); }
    function w(o, l) {
        var s = 0;
        $.each(o.data.vals, function(i, k) { if ($('#' + k).data('notr')) s++; });
        if (s == 0) l();
    }
    function m(o, k) {
        var sd = {};
        if (o.v && !k) sd.v = o.v.val();
        for (var i in o.data.keys) sd[o.data.keys[i]] = $('#' + o.data.vals[i]).val();
        for (var j in o.pars.keys) sd[o.pars.keys[j]] = o.pars.vals[j];
        return sd;
    }
    function sl(o) { o.before("<div class='awf-loading'><span></span></div>"); }
    function el(o) { o.prev().remove(); }
    function p(e) { e.preventDefault(); }
    function n(o) {
        o.v = $('#' + o.i);
        o.f = o.v.parent();
        o.d = o.f.find(".awf-display");
    }
    function outerH(s) {
        var sel = $(s);
        return sel.outerHeight(true) - sel.height();
    }
    function fullscreen(o) {
        $(window).bind("resize", function() { $(o).dialog("option", { height: $(window).height() - 50, width: $(window).width() - 50 }).trigger('dialogresize'); });
    }
    function lookupSearch(o, si) {
        var sd = m(o, o);
        sd.search = $('#' + o.div + ' .awf-search [name="search"]').val();
        sd.selected = si;
        sd.page = 1;

        sl(o.srl);
        $.get(o.searchUrl, sd, function(da) {
            o.srl.empty();
            var jd = awf.readd(da);
            fillsrl(o, jd, sd);
        }).complete(function() { el(o.srl); })
          .error(function() { o.srl.parent().append(awf.errMsg()); });
    }

    function fillsrl(o, jd, sd) {
        if (jd == null) return;
        $.each(jd.Items, function(ii, j) {
            var b = o.mode ? "" : "<button type='button' class='awf-lookup-movebtn'><span></span></button>";
            var bs = "";
            if (o.bs) {
                bs += "<div class=\"awf-list-buttons\">";
                for (var u in o.bs)
                    bs += "<button type='button' class='" + o.bs[u].CssClass + "' onclick='javascript:" + o.bs[u].JsFunc + "(\"" + escape(j.K) + "\")' >" + o.bs[u].Content + "</button>";
                bs += "</div>";

            }

            var x = $("<div>" + j.C + "</div>");
            if (x.find('.awf-list-buttons').length) {
                x.find('.awf-list-buttons').replaceWith(bs);
                j.C = x.get(0).innerHTML;
                bs = '';
            }

            o.srl.append("<li class='awf-list-item' data-val='" + j.K + "'>" + b + j.C + bs + " </li>");

        });

        if (jd.More) {
            o.srl.append(
                    $('<li></li>')
                        .append($('<div class="awf-morebtn">' + o.mt + '</div>')
                                .click(function() {
                                    sd.page++;
                                    sd.isMoreClick = true;
                                    o.f.trigger('awfpage', sd.page);
                                    var x = $(this);
                                    x.after("<div class='awf-loading'><span></span></div>");
                                    x.hide();
                                    $.get(o.searchUrl, sd, function(da) { fillsrl(o, awf.readd(da), sd); })
                                        .complete(function() { x.parent().remove(); })
                                        .error(function() { o.srl.append(awf.errMsg()); });
                                })));

        };
    }

    return {
        errMsg: function() { return "error"; },
        ajaxList: function(o) {
            n(o);
            o.srl = o.d;
            o.mode = 's';
            for (var l in o.data.vals) $('#' + o.data.vals[l])
                .keydown(function(e) { if (e.which == '13') { p(e); } })
                .keyup(function(e) { if (e.which == '13') { p(e); search(1); } });
            o.f.bind('awfpage', function(x, page) { o.v.val(page); });
            function search(pa) {
                var sd = m(o);
                sd.page = parseInt(o.v.val());
                if (pa) {
                    sd.page = pa;
                    o.v.val(pa);
                }

                sl(o.srl);
                $.get(o.searchUrl, sd, function(da) {
                    o.srl.empty();
                    var jd = awf.readd(da);
                    fillsrl(o, jd, sd);
                }).complete(function() { el(o.srl); })
                  .error(function() { o.srl.append(awf.errMsg()); });
            }
            $('#' + o.i + " .awf-list-buttons").live('click', p);
            if (o.sb.length != 0) $('#' + o.sb).click(function(e) { p(e); search(1); });
            search();
        },

        checkboxList: function(o) {
            n(o);
            function l() {
                sl(o.d);
                $.get(o.url, m(o), function(d) {
                    o.d.empty();
                    var jd = awf.readd(d);
                    if (jd != null)
                        $.each(jd, function(i, j) {
                            var dis = o.e ? "" : "disabled='disabled'";
                            o.d.append('<li><input ' + dis + (j.S ? 'checked="checked"' : "")
                        + 'id="' + o.i + '_item_' + i + '" value="' + j.V + '" type="checkbox"/><label for="' + o.i + '_item_' + i + '">' + j.T + '</label></li>');
                        });
                    var x = JSON.stringify($(o.d.find('input:checked')).map(function() { return $(this).val(); }).get());
                    if (x == "[]") x = '';
                    if (x != o.v.val())
                        o.v.val(x).change();
                }).complete(function() { el(o.d); }).error(function() {
                    o.d.append(awf.errMsg());
                });
            }

            w(o, l);
            r(o, l);
            o.d.delegate('input', 'change', function() {
                var x = JSON.stringify($(o.d.find('input:checked')).map(function() { return $(this).val(); }).get());
                if (x == "[]") x = '';
                o.v.val(x).change();
            });
        },

        radioList: function(o) {
            n(o);
            function l() {
                sl(o.d);
                $.get(o.url, m(o), function(d) {
                    o.d.empty();
                    var jd = awf.readd(d);
                    if (jd != null)
                        $.each(jd, function(i, j) {
                            var dis = o.e ? "" : "disabled='disabled'";
                            var sel = "";
                            if (j.S == true) sel = " checked = 'checked' ";
                            o.d.append("<li><input type='radio' " + sel + dis + " value='" + j.V + "' name='" + o.i + "' id='" + o.i + "_item_" + i + "'/><label for='" + o.i + "_item_" + i + "' >" + j.T + "</label></li>");
                        });
                    var nv = o.d.find('input:checked').val();
                    if (typeof nv === 'undefined') nv = '';
                    if (o.v.val() != nv) o.v.val(nv).change();
                }).complete(function() { el(o.d); }).error(function() { o.d.append(awf.errMsg()); }); ;
            }
            w(o, l);
            r(o, l);
            o.d.delegate('input', 'change', function() {
                o.v.val(o.d.find('input:checked').val()).change();
            });
        },


        lookup: function(o) {
            n(o);
            o.div = o.i + '-awfpw';

            o.mode = 's';
            function l() {
                o.d.html('');
                if (o.v.val()) {
                    sl(o.d);
                    $.get(o.getUrl, m(o), function(d) {
                        var g = awf.readd(d);
                        if (g != null) o.d.html(g.C);
                    }).complete(function() { el(o.d); })
                        .error(function() { o.d.parent().append(awf.errMsg()); });
                }
            }
            l();
            o.v.change(l);
            o.f.find('.awf-lookup-clearbutton').click(function(e) { p(e); o.v.val('').change(); });

            o.f.find('.awf-lookup-openbutton').click(function(e) {
                p(e);
                if ($('#' + o.div).length != 0) return;
                $('<div id="' + o.div + '" class="awf-lookup-popup awf-lookup"><div class="awf-search"></div><div class="awf-list awf-lookup-searchlist"><ul></ul></div></div>').hide().appendTo('body');
                awf.popup({ i: o.div, w: o.w, h: o.h, fulls: o.fulls, pos: 'center', res: false, title: o.title, m: o.m,
                    btns: [{ text: o.ok, click: function() {
                        o.v.val($('#' + o.div + ' .awf-lookup-selected').data('val')).change();
                        $(this).dialog('close');
                    }
                    },
                        { text: o.cancel, click: function() { $(this).dialog('close'); } }]
                });

                $('#' + o.div).dialog('open');
                o.srl = $('#' + o.div + ' .awf-lookup-searchlist ul');
                $('#' + o.div + ' .awf-list-item')
                .die('click')
                .live('click', function() {
                    $('#' + o.div + ' .awf-list-item').removeClass('awf-lookup-selected');
                    $(this).addClass('awf-lookup-selected');
                })
                .die('dblclick')
                .live('dblclick', function() {
                    o.v.val($(this).data('val')).change();
                    $('#' + o.div).dialog('close');
                });

                $('<button type="button" class="awf-lookup-searchbutton">' + o.st + '</button>')
                .click(function() { lookupSearch(o, '[]'); })
                .appendTo('#' + o.div + ' .awf-search');

                $('<input type="text" name="search"/>').prependTo('#' + o.div + ' .awf-search')
                .keyup(function(x) { if (x.which == '13') $('#' + o.div).find(".awf-lookup-searchbutton").click(); });
                lookupSearch(o, '[]');

                $('#' + o.div).bind('dialogresize', lay);
                lay();
            });

            function lay() {
                var x = o.div;
                var av = $("#" + x).height() - ($('#' + x + " .awf-search:first").outerHeight()
            + outerH('#' + x + ' .awf-lookup-searchlist:first') + 10);
                $('#' + x + ' .awf-list').css('height', (av * 1) + 'px');
            }
        },

        ajaxDropdown: function(o) {
            n(o);
            function l() {
                sl(o.d);
                $.get(o.url, m(o),
                function(d) {
                    o.d.empty();
                    var g = awf.readd(d);
                    if (g != null)
                        $.each(g, function(q, x) {
                            var sel = "";
                            if (x.S == true) sel = "selected = 'selected'";
                            o.d.append("<option " + sel + " value=\"" + x.V + "\">" + x.T + "</option>");
                        });

                    o.d.change();
                    o.v.data('notr', '');
                    o.v.trigger('awfready');
                }).complete(function() { el(o.d); })
                  .error(function() { o.d.after(awf.errMsg()); });
            }
            w(o, l);
            r(o, l);
            o.d.keyup(function() { $(this).change(); })
               .change(function() {
                   if (o.v.val() != o.d.val())
                       o.v.val(o.d.val()).change();
               });
        },

        readd: function(d) { if ((typeof (d.d) == 'object' || typeof (d.d) == 'string') && d.d != null) { return d.d; } return null; },

        popup: function(o) {
            draggable = true;
            if (o.fulls) { o.res = false; o.m = true; }
            if (o.m || o.fulls) draggable = false;
            $("#" + o.i).dialog({
                draggable: draggable,
                width: o.fulls ? $(window).width() - 50 : o.w,
                height: o.fulls ? $(window).height() - 50 : o.h,
                title: o.title,
                modal: o.m,
                position: o.pos,
                resizable: o.res,
                buttons: o.btns,
                autoOpen: false,
                close: function() { $("#" + o.i).find('*').remove(); $(this).remove(); }
            });

            $("#" + o.i).dialog("option", { dialogClass: 'awf-fixed' });
            if (o.fulls) fullscreen("#" + o.i);
        },

        multilookup: function(o) {
            n(o);
            o.div = o.i + '-awfpw';

            o.v.change(l);
            l();
            o.f.find('.awf-lookup-clearbutton').click(function(e) { p(e); o.v.val('').change(); });
            o.f.find('.awf-lookup-openbutton').click(function(e) {
                p(e);
                if ($('#' + o.div).length != 0) return;

                $('<div id="' + o.div + '" class="awf-lookup-popup awf-multilookup"><div class="awf-search"></div><div class="awf-list awf-lookup-searchlist"><ul></ul></div><div class="awf-list awf-lookup-selectedlist" ><ul></ul></div></div>').hide().appendTo('body');

                awf.popup({ i: o.div, w: o.w, h: o.h, fulls: o.fulls, pos: 'center', res: false, title: o.title, m: o.m,
                    btns: [{ text: o.ok, click: function() {
                        o.v.val(gs()).change();
                        $(this).dialog('close');
                    }
                    },
                        { text: o.cancel, click: function() { $(this).dialog('close'); } }]
                });
                $('#' + o.div).dialog('open');
                o.srl = $('#' + o.div + ' .awf-lookup-searchlist ul');
                o.sel = $('#' + o.div + ' .awf-lookup-selectedlist ul');

                o.srl.delegate('.awf-lookup-movebtn', 'click', add);
                o.sel.delegate('.awf-lookup-movebtn', 'click', rem);

                $('<button type="button" id="' + o.i + 'sbtn" >' + o.st + '</button>')
                .click(function() { lookupSearch(o, gs()); })
                .appendTo('#' + o.div + ' .awf-search');

                $('<input type="text" name="search"/>').prependTo('#' + o.div + ' .awf-search')
                .keyup(function(x) { if (x.which == '13') $('#' + o.i + 'sbtn').click(); });

                lookupSearch(o, o.v.val());

                if (o.v.val()) {
                    sl(o.sel);
                    var sd = m(o, o);
                    sd.selected = o.v.val();
                    $.get(o.selectedUrl, sd, function(d) {
                        var g = awf.readd(d);
                        if (g != null) {
                            $.each(g, function(i, j) {
                                o.sel.append("<li class='awf-list-item' data-val='" + j.K + "'><button type='button' class='awf-lookup-movebtn'><span></span></button>" + j.C + " </li>");
                            });
                        }

                    }).complete(function() { el(o.sel); })
                      .error(function() { o.sel.parent().append(awf.errMsg()); });
                }

                $('#' + o.div).bind('dialogresize', lay); lay();
            });


            $('#' + o.sel + ' .awf-lookup-movebtn').live('click', rem);
            function add() { $(this).closest('.awf-list-item').prependTo(o.sel); }
            function rem() { $(this).closest('.awf-list-item').prependTo(o.srl); }
            function lay() {
                var y = o.div;
                var av = $("#" + y).height() - ($('#' + y + " .awf-search").outerHeight(true)
                + outerH('#' + y + ' .awf-lookup-searchlist')
                + outerH('#' + y + ' .awf-lookup-selectedlist') + 10);

                $('#' + y + ' .awf-list').css('height', (av * 0.5) + 'px');
            }

            function gs() { return JSON.stringify(o.sel.find('li').map(function() { return $(this).data('val'); }).get()); }

            function l() {
                var x = o.d.empty();
                var v = o.v.val();
                if (v) {
                    sl(x.parent());
                    var sd = m(o, o);
                    sd.v = v;
                    $.get(o.getMultipleUrl, sd, function(d) {
                        var g = awf.readd(d);
                        if (g != null)
                            $.each(g, function(q, j) {
                                x.append('<li>' + j.C + '</li>');
                            });
                    }).complete(function() { el(x.parent()); })
                          .error(function() { x.parent().append(awf.errMsg()); });
                }
            }
        }
    };
} ();